using System;
using System.Runtime.InteropServices;
using Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData;

namespace Scenario.WAPI
{
    public partial class ApplicationHelper
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="Application"/> class.
		/// </summary>
        public ApplicationHelper()
		{
		}

        public void RunAllApplications()
        {
            Response wapiResponse = new Response();
            string sXML = ApplicationHelper.GetApplications(ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                return;
            }

            XMLApplications xmlApplications = new XMLApplications();
            xmlApplications.Parse(sXML);

            XMLApplication xmlApplication = null;
            for (int i = 0; i < xmlApplications.Count; i++)
            {
                xmlApplication = (XMLApplication)xmlApplications[i];

                if (xmlApplication.Type == XMLApplication.EXECUTABLE_TYPE)
                {
                    String sFileName = System.IO.Path.GetFileName(xmlApplication.LibraryFile);
                    String sFilePath = System.IO.Path.GetDirectoryName(sFileName);
                    bOS.Microsoft.Windows.ProcessHandler.Start(sFileName, xmlApplication.LibraryFile, sFilePath, true);
                }
            }
        }

        public XMLApplications GetApplications()
        {
            Response wapiResponse = new Response();
            string sXML = ApplicationHelper.GetApplications(ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }

            XMLApplications xmlApplications = new XMLApplications();
            xmlApplications.Parse(sXML);

            return xmlApplications;
        }
    }
}
